cc.Class({
    extends: cc.BaseClass,

    properties: {
        ediboxArr:      [cc.EditBox],
        btnRegister:    cc.Button,     
    },

    start () {
        this.playMoveAction();
        this.initData();
    },

    initData:function(){
        this.btnGetCodeNode = this.ediboxArr[1].node.parent.getChildByName("btnGetCode");
        this.btnGetCodeLab = this.ediboxArr[1].node.parent.getChildByName("labTime");
        this.btnGetCodeLab.active = false;
        this.timer = GETCODETIME;

        this.phoneCode  = 0;
    },

    onBtnClickCallBack:function(event,customData){
        var type = customData;
        if(type == "register"){
            this.resgister();
        }else if(type == "getCode"){
            this.getCode();
        }
    },

    //获取验证码
    getCode:function(){
        var accout = this.ediboxArr[0].string;
        if(accout == "" || accout.length < 11){
            G.showTip(LNG.PLACELOGIN);
            return;  
        }

        this.btnGetCodeLab.active = true;
        this.btnGetCodeLab.getComponent(cc.Label).string = this.timer; 
        this.btnGetCodeNode.getComponent(cc.Button).interactable = false;

        this.schedule(this._setgetColdTimer,1);
        WebCenter.getPhoneCode(accout,function(data){
            var item = JSON.parse(data);
            if(item.Res == 1){
                this.phoneCode = item.RandNum;
            }
            G.showTip(item.Describe);  
        }.bind(this));
    },

    _setgetColdTimer :function(){
        this.timer--;
        if(this.timer < 0){
            this.unschedule(this._setgetColdTimer);
            this.btnGetCodeLab.active = false;
            this.btnGetCodeNode.getComponent(cc.Button).interactable = true;
        }
        this.btnGetCodeLab.getComponent(cc.Label).string = this.timer; 
    },

    //注册
    resgister:function(){
        var phone = this.ediboxArr[0].string;
        var code = this.ediboxArr[1].string;
        var pwd = this.ediboxArr[2].string;
        var pwd2 = this.ediboxArr[3].string;

        if(phone == "" || phone.length < 11){
            G.showTip(LNG.PLACELOGIN);
            return;
        }
        if(code != this.phoneCode || this.phoneCode.length < 4){
            G.showTip(LNG.LOGINCODE_ERR);
            return;
        }
        if(pwd != pwd2){
            G.showTip(LNG.REGISTERPWDERR);
            return;
        }

        this.btnRegister.interactable = false;

        WebCenter.loginGameByPhone(phone,code,function(data){
            var item = JSON.parse(data);
            if(item.Res == 0){
                ClientNotify.send(CLIENT_NOTIFY_TYPE.GOTOLOGIN, item);
            }
            this.btnRegister.interactable = true;
            G.showTip(item.Describe);  
        }.bind(this),LOGINTYPE.PHONEPASSWORDREGISTERLOGIN);
    },

    onDestroy:function(){
        if(this._setgetColdTimer){
            this.unschedule(this._setgetColdTimer);
        }
        this.phoneCode = 0;
    }
});
